package com.educatioin.manage.wechat.controller;

import com.educatioin.manage.common.beans.Response;
import com.educatioin.manage.web.pointreward.po.PointRewardOrderPo;
import com.educatioin.manage.web.pointreward.po.PointRewardProductPo;
import com.educatioin.manage.web.pointreward.repository.PointRewardOrderRepository;
import com.educatioin.manage.web.pointreward.repository.PointRewardProductRepository;
import com.educatioin.manage.wechat.request.CreatePROrderReq;
import com.educatioin.manage.wechat.request.QueryPRProductsReq;
import com.educatioin.manage.wechat.response.*;
import com.educatioin.manage.wechat.service.WeChatPROrderService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangyin on 2018/5/1.
 */
@RestController
@RequestMapping("/wx/api/pointReward")
public class WeChatPointRewardController {

    @Autowired
    private WeChatPROrderService weChatPROrderService;
    @Resource
    private PointRewardOrderRepository pointRewardOrderRepository;
    @Resource
    private PointRewardProductRepository pointRewardProductRepository;

    /**
     * 获取产品列表
     * @return
     */
    @RequestMapping("/queryProducts")
    @ResponseBody
    public QueryPRProductsRsp queryProducts(QueryPRProductsReq queryPRProductsReq) {
        QueryPRProductsRsp queryProductsRsp = new QueryPRProductsRsp();

        Integer page = queryPRProductsReq.getPage();
        Integer limit  = queryPRProductsReq.getLimit();
        if(null == page|| page<=0 || null == limit || limit< 0){
            queryProductsRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            queryProductsRsp.setReturnMsg("分页查询错误！");
            return queryProductsRsp;
        }

        if(page > 0) {
            page = page -1;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageRequest = new PageRequest(page, limit, sort);

        Page<PointRewardProductPo> retPage = pointRewardProductRepository.findAll((root, query, cb) -> {
            List<Predicate> predicates = Lists.newArrayList();
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }, pageRequest);
        queryProductsRsp.setReturnCode(Response.RETURN_SUCCESS_CODE);
        if(retPage.hasContent()) {
            queryProductsRsp.setData(retPage.getContent());
        }

        queryProductsRsp.setCount((int)retPage.getTotalElements());
        queryProductsRsp.setReturnCode(Response.RETURN_SUCCESS_CODE);
        return queryProductsRsp;
    }

    /**
     * 根据Id获取产品详情
     * @param pointRewardProductId
     * @return
     */
    @RequestMapping("/getProductById")
    @ResponseBody
    public GetPRProductByIdRsp getProductById(Long pointRewardProductId) {
        GetPRProductByIdRsp getProductByIdRsp = new GetPRProductByIdRsp();

        PointRewardProductPo pointRewardProductPo = pointRewardProductRepository.findOne(pointRewardProductId);
        if(null == pointRewardProductPo){
            getProductByIdRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            getProductByIdRsp.setReturnMsg("产品不存在！");
            return getProductByIdRsp;
        }

        getProductByIdRsp.setReturnCode(Response.RETURN_SUCCESS_CODE);
        getProductByIdRsp.setPointRewardProductPo(pointRewardProductPo);
        //调用查询接口获取数据
        return getProductByIdRsp;
    }

    /**
     * 创建订单接口
     * @param createOrderReq
     * @return
     */
    @RequestMapping("/createOrder")
    @ResponseBody
    public CreateOrderRsp createOrder(HttpServletRequest request, @RequestBody CreatePROrderReq createOrderReq) {
        return weChatPROrderService.createOrder(request, createOrderReq);
    }

    /**
     * 支付订单接口
     * @param orderCode
     * @return
     */
    @RequestMapping("/payOrder")
    @ResponseBody
    public Response payOrder(HttpServletRequest request, String orderCode) {
        return weChatPROrderService.payOrder(request, orderCode);
    }

    /**
     * 获取订单列表
     * @return
     */
    @RequestMapping("/queryOrders")
    @ResponseBody
    public QueryPROrdersRsp queryOrders(HttpServletRequest request) {
        return weChatPROrderService.queryOrdersByToken(request);
    }

    /**
     * 获取订单详情接口
     * @param orderCode
     * @return
     */
    @RequestMapping("/getPROrderByCode")
    @ResponseBody
    public GetPROrderByCodeRsp getOrderByCode(String orderCode) {
        GetPROrderByCodeRsp getPROrderByCodeRsp = new GetPROrderByCodeRsp();

        PointRewardOrderPo pointRewardOrderPo = pointRewardOrderRepository.findByOrderCode(orderCode);
        if(null == pointRewardOrderPo){
            getPROrderByCodeRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            getPROrderByCodeRsp.setReturnMsg("订单不存在！");
            return getPROrderByCodeRsp;
        }

        //根据productCode获取产品
        PointRewardProductPo pointRewardProductPo = pointRewardProductRepository.findOne(pointRewardOrderPo.getPrProductId());
        if (null == pointRewardProductPo) {
            getPROrderByCodeRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            getPROrderByCodeRsp.setReturnMsg("产品不存在！");
            return getPROrderByCodeRsp;
        }

        getPROrderByCodeRsp.setReturnCode(Response.RETURN_SUCCESS_CODE);
        getPROrderByCodeRsp.setName(pointRewardOrderPo.getName());
        getPROrderByCodeRsp.setCustomerName(pointRewardOrderPo.getCustomerName());
        getPROrderByCodeRsp.setPrImage(pointRewardOrderPo.getPrImage());
        getPROrderByCodeRsp.setPhone(pointRewardOrderPo.getPhone());
        getPROrderByCodeRsp.setAddress(pointRewardOrderPo.getAddress());
        getPROrderByCodeRsp.setAmount(pointRewardOrderPo.getAmount());
        getPROrderByCodeRsp.setTotalPrice(pointRewardOrderPo.getTotalPrice());

        getPROrderByCodeRsp.setOrderStatus(pointRewardOrderPo.getOrderStatus());
        getPROrderByCodeRsp.setOrderTime(stampToDate(pointRewardOrderPo.getCreateTime()));

        return getPROrderByCodeRsp;
    }

    private String stampToDate(Long timeStamp){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(timeStamp);
        return simpleDateFormat.format(date);
    }
}
